package com.zx.article.util;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.lang.reflect.Field;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Collection;
import java.util.Map;
import java.util.Map.Entry;

import javax.servlet.http.HttpServletRequest;

import org.json.JSONArray;
import org.json.JSONObject;

import sun.misc.BASE64Encoder;

public class FmtRequest {

	/**
	 * request提炼出model
	 * 
	 * @param <T>
	 * @param request
	 * @param cls
	 * @return
	 */
	public <T> T parseModel(HttpServletRequest request, Class<T> cls) {
		T obj = null;
		try {
			obj = cls.newInstance();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		}
		Map<String, String[]> parameterMap = request.getParameterMap();
		for (Entry<String, String[]> entry : parameterMap.entrySet()) {
			if ("action".equals(entry.getKey()) || "realPassword".equals(entry.getKey())
					|| "pageLimit".equals(entry.getKey()) || "pageIndex".equals(entry.getKey())
					|| "authcode".equals(entry.getKey())) {
				continue;
			}
			Field declaredField;
			try {
				declaredField = cls.getDeclaredField(entry.getKey());
				declaredField.setAccessible(true);
				Object value = "password".equals(entry.getKey()) ? encode(entry.getValue()[0]) : entry.getValue()[0];
				declaredField.set(obj, value);
			} catch (NoSuchFieldException e) {
				e.printStackTrace();
			} catch (SecurityException e) {
				e.printStackTrace();
			} catch (IllegalArgumentException e) {
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			}
		}
		return obj;
	}

	/**
	 * 加密
	 * 
	 * @param string
	 * @return
	 */
	public String encode(String string) {
		if (string == null || string.isEmpty()) {
			return null;
		}
		try {
			MessageDigest messageDigest = MessageDigest.getInstance("MD5");
			BASE64Encoder base64Encoder = new BASE64Encoder();
			return base64Encoder.encode(messageDigest.digest(string.getBytes("utf-8")));
		} catch (NoSuchAlgorithmException | UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 格式化数据写出
	 * 
	 * @param writer
	 * @param msgString
	 */
	public void write(Writer writer, Object msgString) {
		if (msgString instanceof String) {
			write(writer, msgString.toString());
		} else if (msgString instanceof Collection<?>) {
			write(writer, new JSONArray((Collection<?>) msgString).toString());
		} else if (msgString instanceof Map<?, ?>) {
			write(writer, new JSONObject((Map<?, ?>) msgString).toString());
		} else if (msgString instanceof Object) {
			write(writer, new JSONObject(msgString).toString());
		}
	}

	/**
	 * 字符写出
	 * 
	 * @param writer
	 * @param msgString
	 */
	public void write(Writer writer, String msgString) {
		try {
			writer.write(msgString);
			writer.flush();
			writer.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		writer = null;
	}

}
